package com.zxm.controller;
/**
 * @author 张鑫铭
 */

import com.alibaba.dubbo.config.annotation.Reference;
import com.zxm.pojo.SportDetail;
import com.zxm.pojo.UserInfo;
import com.zxm.result.EasyUiResult;
import com.zxm.result.GuhealthResult;
import com.zxm.service.SportDetailService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sportDetail")
public class SportDetailController {
	//用户查看运动数据中心
	@Reference
    private SportDetailService sportDetailService;

    //根据条件查询用户身体数据（一段时间范围内,指定运动类型（室内、室外、骑行））
    @RequestMapping("/getSportDetailByCondition")
    @ResponseBody
    public List<SportDetail> getUserBodyInfo(String start,String end,UserInfo userInfo ,String sportType) throws ParseException {
        //不做非空校验/在前台强制要求指定运动类型
        //end可以为空
        if(end==null) {
            return sportDetailService.showSportDetailByCondition(java.sql.Date.valueOf(start),null, userInfo.getUsername(), sportType);
        }
        return sportDetailService.showSportDetailByCondition(java.sql.Date.valueOf(start), java.sql.Date.valueOf(end), userInfo.getUsername(), sportType);
    }

    /**
     * 根据username 插入一条运动数据
     */
    @RequestMapping("/addSportDetailByConditon")
    @ResponseBody
    public GuhealthResult addSportDetailByConditon(String username,SportDetail sportDetail){
        return sportDetailService.addSportDetailByConditon(username,sportDetail);
    }
    //根据正在运动的用户，拿到他最新的一条运动信息用于展示折线图
    @RequestMapping("/getNewSportDetail")
    @ResponseBody
    public Map<String,Object> selectByNewSportDetail(String username) {
        Map<String,Object> map =new HashMap<>();
        SportDetail sport=sportDetailService.getNewSportDetail(username);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date=sdf.format(sport.getStartTime());
        Date sportdate=null;
        try {
            sportdate = sdf.parse(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        sport.setStartTime(sportdate);
        map.put("sportTime", date);
        map.put("sportDetial", sport);
        return 	map;
    }
    /**
     * 查询出当前正在运动 或者 历史用户
     */
    @RequestMapping("/getSportUser")
    @ResponseBody
    public EasyUiResult getSportUser(@RequestParam(defaultValue="1") Integer page,
                                     @RequestParam(defaultValue="15") Integer rows,String sportType, String status){
        EasyUiResult userList = sportDetailService.getSportUser(page,rows,sportType,status);
        return userList;
    }
}
